package com.linshidream.intrachat.simplenio.pipeline;

import com.linshidream.intrachat.simplenio.pipeline.context.MiniHandlerContext;
import com.linshidream.intrachat.simplenio.pipeline.handler.MiniChannelInboundHandler;

/**
 * Created on 2025/8/17 21:48
 *
 * @author linshidream
 * @version 1.0.0
 * @description
 */

public class ServerEchoHandler implements MiniChannelInboundHandler {

    @Override
    public void channelRead(MiniHandlerContext ctx, Object msg) {
        if (msg instanceof String) {
            // 下面就是业务处理，我这里简单的只是把消息再次返回，实际场景中根据业务自行开发
            try {
                System.out.println(ctx.handlerName() + ", 服务端收到客户端消息:" + msg);
                String respMsg = "Echo:" + msg;
                System.out.println(ctx.handlerName() + ", 服务端响应客户端消息:" + respMsg);

                // 此处不再需要编码 走 outbound pipeline
                ctx.channel().write("echo: " + msg);

                ctx.fireChannelRead(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}
